'use strict';

angular.module('app')
    .controller('aboutUsDetailController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "关于我们详情";
            var id = $state.params.id;
            var manageId = $state.params.manageId;
            $scope.title = $rootScope.title = title;
            $scope.url="https://ifci.lincomb.com";//本地也显示图片
            $scope.loading = true;
            UE.delEditor("intro");
            var ue = UE.getEditor('intro',{
                serverUrl:"/iFengChao-SYS-Web/upload/uploadUMImage"

            });

            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/aboutus/read/detail',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                        $scope.imgData = [];
                        var srcArr = [];
                        srcArr.push($scope.record.smallIconUrl);
                        $scope.imgData.push({
                            src:srcArr
                        })
                        console.log($scope.imgData);
                        ue.setContent($scope.record.afficheContent);
                        $scope.$apply();
                    } else {
                        $scope.msg = result.msg;
                    }

                });
            }

            $scope.submit=function() {

                var picUrl = $scope.imgData[0];
                if (!picUrl) {
                    $scope.alertModal({'content':'请上传封面图片'});
                    return;
                }
                var picUrl = $scope.imgData[0].src;
                $scope.record.smallIconUrl = picUrl[0];
                $scope.record.id=id;
                $scope.record.manageId=manageId;
                $scope.isDisabled = true;//提交disabled
                $scope.record.intro= UE.getEditor('intro').getContent();
                var m = $scope.record;
                $.ajax({
                    url:  $scope.app.project_name + '/aboutus/update/updateAboutUs',
                    data: m
                }).then(function (result) {
                    if (result.httpCode == 200) {
                        if (result.data > 0) {
                            $scope.alertModal({"content": "修改成功"});
                            $scope.isDisabled = false;
                            $scope.loading = false;
                            $state.go('main.web.aboutus.list');
                            $scope.$apply();
                        } else {
                            $scope.alertModal({"content": "该栏目名称已存在"});
                            $scope.isDisabled = false;
                            $scope.loading = false;
                            $scope.$apply();
                        }
                    } else {
                        $scope.alertModal({"content": "修改失败"});
                        $scope.isDisabled = false;
                        $scope.loading = false;
                        $state.go('main.web.aboutus.list');
                        $scope.$apply();
                    }


                });
            };

            //表单验证
            function validate(userId,manageId){
                $.validator.addMethod('notEqual', function(value, ele){
                    return value != this.settings.rules[ele.name].notEqual;
                });
                jQuery('form').validate({
                    rules: {
                        manageName: {
                            required: true
                        },
                        title: {
                            required: true
                        },
                        description:{
                            required: true
                        },
                        keywords:{
                            required: true
                        }

                    },
                    messages: {
                        manageName: {
                            required: '栏目不能为空'
                        },
                        title: {
                            required: '标题不能为空'
                        },
                        description:{
                            required: '描述不能为空'
                        },
                        keywords:{
                            required: '关键词不能为空'
                        }
                    },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }

            activate(id);
            validate(id,manageId);


        }]);